<?php

require_once 'cms/menu/om/om/BaseMenuItem.php';


/**
 * Skeleton subclass for representing a row from the 'menu_item' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package menu.om
 */	
class MenuItem extends BaseMenuItem {


    /**
     * Get the name of this menuitemI18n in the CMS default language.
     *
     * @param default The default CMS language code.
     */
    function getDefaultName($default) {
        $c = new Criteria();
        $c->clearSelectColumns();
        $c->addSelectColumn(MenuItemI18nPeer::NAME);
        $c->add(MenuItemI18nPeer::MENU_ITEM_ID, $this->getMenuItemId());
        $c->add(MenuItemI18nPeer::LANGUAGE_CODE, $default);
        $c->setLimit(1);
        $rs = MenuItemI18nPeer::doSelectRs($c);
        if ($rs->next())
            return $rs->getString(1);

    }


    function getMaxPosition() {

        $con = Propel::getConnection();
        $stmt = $con->createStatement();

        $parentId = $this->getParentId() == 0 ? null : $this->getParentId();

        $rs = $stmt->executeQuery(
                "SELECT MAX(position) FROM menu_item " .
                "WHERE menu_id = ". $this->getMenuId() . " AND parent_id " . (is_null($parentId) ?
                    " IS NULL" : " = '".$parentId."'"),
                ResultSet::FETCHMODE_NUM
              );

        if($rs->next()) {
            return $rs->getInt(1);
        }
    }

    /*
     * OVERRIDE THIS FROM THE BASE CLASS. OTHERWISE POPULATING FORM WILL NOT WORK IN ACTIONCONTROLLER
     * WHEN FOREIGN KEY OBJECT ARE USED
     */
    public function setMenuId($v)
    {
        if ($this->menu_id !== $v) {
            $this->menu_id = $v;
            $this->modifiedColumns[] = MenuItemPeer::MENU_ID;
        }
    }


} // MenuItem
